package com.me.launcher.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


import com.me.launcher.bean.AppEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * @author hejiangwei
 * @date 2018/3/30
 * 安装与卸载的广播接收者
 * 使用了静态注册
 */

public class AppReceiver extends BroadcastReceiver {

    private static final String TAG = AppReceiver.class.getName();
    @Override
    public void onReceive(Context context, Intent intent) {
        //接收安装广播
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
            String data = intent.getDataString();
            String packageName = data.substring(data.indexOf(":") + 1);
            EventBus.getDefault().post(new AppEvent(0, packageName));
        }

        //接收更新广播
        if (intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")) {
            String data = intent.getDataString();
            String packageName = data.substring(data.indexOf(":") + 1);
            Log.e(TAG, "onReceive====="+packageName );
        }

        //接收卸载广播
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
            String data = intent.getDataString();
            String packageName = data.substring(data.indexOf(":") + 1);
            EventBus.getDefault().post(new AppEvent(1, packageName));

        }
    }
}